<?php
class Img
{
    private $src;

    private $image;

    private $imageinfo;

    private $percent = 0.5;

    /**
     * 图片压缩
     *
     * @param $src 源图            
     * @param float $percent
     *            压缩比例
     */
    public function __construct($src, $percent = 1)
    {
        $this->src = $src;
        $this->percent = $percent;
    }

    /**
     * 高清压缩图片
     *
     * @param string $saveName
     *            提供图片名（可不带扩展名，用源图扩展名）用于保存。或不提供文件名直接显示
     * @param int $maxFileSize
     *            目标文件大小
     */
    public function compressImg($saveName = '')
    {
        $this->_openImage();
        if (! empty($saveName))
            
            $this->_saveImage($saveName); // 保存
        else
            $this->_showImage();
        
        return true;
    }

    /**
     * 内部：打开图片
     */
    private function _openImage()
    {
        list ($width, $height, $type, $attr) = getimagesize($this->src);
        $this->imageinfo = array(
            'width' => $width,
            'height' => $height,
            'type' => image_type_to_extension($type, false),
            'attr' => $attr
        );
        $fun = "imagecreatefrom" . $this->imageinfo['type'];
        $this->image = $fun($this->src);
        
        $this->_thumpImage();
    }

    /**
     * 内部：操作图片
     */
    private function _thumpImage()
    {
        
        // $this->percent = $this->imageinfo['width'] ;
        $new_width = $this->imageinfo['width'] * $this->percent;
        $new_height = $this->imageinfo['height'] * $this->percent;
        
        // $new_width = 1200;
        // $new_height = 900;
        
        $image_thump = imagecreatetruecolor($new_width, $new_height);
        // 将原图复制带图片载体上面，并且按照一定比例压缩,极大的保持了清晰度
        imagecopyresampled($image_thump, $this->image, 0, 0, 0, 0, $new_width, $new_height, $this->imageinfo['width'], $this->imageinfo['height']);
        imagedestroy($this->image);
        $this->image = $image_thump;
        
//         $this->image = imagerotate($this->image, 270, 0);
    }

    /**
     * 输出图片:保存图片则用saveImage()
     */
    private function _showImage()
    {
        header('Content-Type: image/' . $this->imageinfo['type']);
        $funcs = "image" . $this->imageinfo['type'];
        $funcs($this->image);
    }

    /**
     * 保存图片到硬盘：
     *
     * @param string $dstImgName
     *            1、可指定字符串不带后缀的名称，使用源图扩展名 。2、直接指定目标图片名带扩展名。
     */
    private function _saveImage($dstImgName)
    {
        if (empty($dstImgName))
            return false;
        $allowImgs = [
            '.jpg',
            '.jpeg',
            '.png',
            '.bmp',
            '.wbmp',
            '.gif'
        ]; // 如果目标图片名有后缀就用目标图片扩展名 后缀，如果没有，则用源图的扩展名
        $dstExt = strrchr($dstImgName, ".");
        $sourseExt = strrchr($this->src, ".");
        if (! empty($dstExt))
            $dstExt = strtolower($dstExt);
        if (! empty($sourseExt))
            $sourseExt = strtolower($sourseExt);
        
        // 有指定目标名扩展名
        if (! empty($dstExt) && in_array($dstExt, $allowImgs)) {
            $dstName = $dstImgName;
        } elseif (! empty($sourseExt) && in_array($sourseExt, $allowImgs)) {
            $dstName = $dstImgName . $sourseExt;
        } else {
            $dstName = $dstImgName . $this->imageinfo['type'];
        }
        $funcs = "image" . $this->imageinfo['type'];
        $funcs($this->image, $dstName);
    }

    /**
     * 销毁图片
     */
    public function __destruct()
    {
        if(!empty($this->image)){
            imagedestroy($this->image);
        }
    }

    /**
     * 压缩图片到指定大小内
     * @param string $to_source

     *          压缩后保存的路径
     * @param number $maxFileSize
     *            要压缩的大小
     * @return boolean
     */
    public function getimg($to_source, $maxFileSize = 200)
    {
        $source_size = filesize($this->src) / 1024;
        if ($source_size < $maxFileSize) {
            copy($this->src, $to_source);
            return true;
        } else {
            
            $ys = true;
            $i = 0;
            while ($ys) {
                clearstatcache();
                $this->percent = 0.7;
                // $percent = 0.7;
                if ($i > 0) {
                    $this->src = $to_source;
                }
                $image = $this->compressImg($to_source);
                $to_source_size = filesize($to_source) / 1024;
                // var_dump($to_source_size);
                
                $ys = $to_source_size > $maxFileSize;
                $i ++;
            }
            // echo "压缩次数:" . $i;
        }
        return true;
    }
}